import { watch } from "fs";
import router from '../router'
import { Toast } from 'vant';
import axios from 'axios'
import qs from 'qs'
var ToastBox ="";
export default {
    //  
    install (Vue, options){
        Vue.prototype.appEquipment=navigator.userAgent,
        Vue.prototype.getUrl = function (name) { //获取地址参数
            if (window.location.search) { //判断是否hash模式
                var parameter = window.location.search;
            } else {
                var parameter = "?" + window.location.hash.slice(2).split("?")[1];

            }
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = parameter.substr(1).match(reg);
            if (r != null) {
                return decodeURI(r[2]);
            } else {
                return null;
            }
        },
        //精度丢失 除
        Vue.prototype.multiple = function (arg1, arg2) {
            var c, d, e = 0,
                f = 0;
            try {
                e = arg1.toString().split(".")[1].length;
            } catch (g) {}
            try {
                f = arg2.toString().split(".")[1].length;
            } catch (g) {}
            return c = Number(arg1.toString().replace(".", "")), d = Number(arg2.toString().replace(".", "")), this.GLOBAL.accMul(c / d, Math.pow(10, f - e));

        },
        Vue.prototype.$fmthkdate = function (statementDate,repaymentDate){ //计算距离账单日有多长时间 statementDate:账单日 repaymentDate：还款日
			var myDate = new Date().getDate();
			var date = new Date();
			var year = date.getFullYear();
			var month = date.getMonth() + 1;
			var d = new Date(year, month, 0);
			var s = d.getDate();
			// console.log('本月'+s)
			// console.log('今天'+myDate)
			//statementDate:账单日  repaymentDate:还款日 myDate:当前是几号 s:这个月有几天
			
			if (parseInt(statementDate) < parseInt(repaymentDate)) {

				if(statementDate<myDate&&repaymentDate>myDate){ //当前日期 再 还款日与当前日期之前
					var datt = parseInt(repaymentDate) - parseInt(myDate)
				}else{
					var datt = parseInt(repaymentDate) - parseInt(statementDate)
				}
				return datt
			} else if(parseInt(statementDate) > parseInt(repaymentDate)){
				if(statementDate > myDate && repaymentDate > myDate) {
					return repaymentDate * 1 - myDate;
				} else {
					return s * 1 - statementDate * 1 + repaymentDate * 1
				}
			}
		},
        Vue.prototype.jumPage = function (url,type) {  //页面跳转
            router.push(url);
        },
        Vue.prototype.getUrl = function (name){
            if(window.location.search) { //判断是否hash模式
                var parameter = window.location.search;
            } else {
                var parameter = "?" + window.location.hash.slice(2).split("?")[1];
        
            }
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = parameter.substr(1).match(reg);
            if(r != null) {
                return decodeURI(r[2]);
            } else {
                return null;
            }
        },
        Vue.prototype.PrefixInteger = function (num,n){ //数字补零
            return (Array(n).join(0) + num).slice(-n);
        },
        Vue.prototype.commonConfig = {
            errorImg: "this.src='../images/qitayinhang.png'", //银行卡错误图
        },
        Vue.prototype.$axios = function (url,data,type){
            return new Promise((resolve,reject)=>{ 

                //处理为空的数据
                $.each(data,function(k,v){
                    if(v==''||v==null){
                        delete data[k]
                    }
                })

                //显示加载框
                clearTimeout(ToastBox);
                ToastBox =  setTimeout(()=>{
                    Toast.loading({
                        message: '加载中...',
                        forbidClick: true,
                        duration:0
                });
                },100)

                axios({
                    url:url,
                    method:type,
                    params: type === 'get' ? data : null,
                    data: type === 'post' ? qs.stringify(data) : null,
                    headers: {
                        // 'token':localStorage.getItem('token')
                      }
                }).then(res=>{
                    clearTimeout(ToastBox);
                    Toast.clear(); 
                    resolve(res.data);
                }).catch(res =>{
                    clearTimeout(ToastBox);
                    resolve(res.data);
                    Toast('服务器异常');
                    setTimeout(() => {
                        Toast.clear(); 
                    }, 1500);
                })




            })
        }
        
    }
}    